package org.example.k8s_cloudplatform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.entity.res.PasswordChanger;
import org.example.k8s_cloudplatform.entity.res.Result;
import org.example.k8s_cloudplatform.entity.User;
import org.example.k8s_cloudplatform.service.UserService;
import org.example.k8s_cloudplatform.utils.CheckUtil;
import org.example.k8s_cloudplatform.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping(value = "/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }
    @PostMapping(value = "/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }
    @PostMapping(value = "/info")
    public Result info(@RequestHeader("Token") String token ) {
        Integer id= JwtUtil.IDParser(token);
        User res = userService.getById(id);
        res.setPassword("");
        return Result.success(res);
    }

    @GetMapping("/viewUser")
    public Result viewPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<User> userWrapper = Wrappers.lambdaQuery();
        userWrapper.eq(User::getStatus,0);//仅选择已激活用户
        userWrapper.eq(User::getJob,0);//仅选择普通用户
        Page<User> UserPage = userService.page(new Page<>(pageNum, pageSize),userWrapper);
        return Result.success(UserPage);
    }

    @GetMapping("/findUserByID")
    public Result findUserByID(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam String search) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getStatus,0);//仅选择已激活员工
        wrapper.eq(User::getJob,0);//仅选择普通员工
        if (StrUtil.isNotBlank(search)&& CheckUtil.isNum(search)) {
                wrapper.likeRight(User::getID, Integer.parseInt(search));
        }
        Page<User> userPage = userService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }

    @PostMapping("/updateInfo")
    public Result updateInfo(@RequestBody User user) {
        return userService.updateInfo(user);
    }
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordChanger passwordChanger) {
        return userService.updatePassword(passwordChanger);
    }
    @PostMapping("/passwordReset")
    public Result passwordReset(@RequestBody User user) {
        return userService.passwordReset(user);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody User user) {
        return userService.delete(user);
    }

}
